﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;


namespace Valhalla.Emulator
{
    
    public partial class Form1 : Form
    {
        GBCart rom;
        Clock clock;
        Thread clockTread;
        bool fileOpened = false;

        public Form1()
        {
            InitializeComponent();

            openFileDialog1.Filter = "GameBoy files (*.gb)|*.gb|GameBoy Color files (*.gbc)|*.gbc";
            this.FormClosing += this.Form1_FormClosing;
            
        }

        private void LoadData(string filename)
        {
            FileInfo fi = new FileInfo(filename);
            if (!fi.Exists)
                throw new FileNotFoundException();
            if (fi.Length > 4 * 1024 * 1024)
                throw new WrongRomFormatException();

            rom = new GBCart(filename);
            fileOpened = true;

            if (!rom.Runable)
            { MessageBox.Show("This cartrige is not ROM Only, so it cannot be played in our emulator.", "ERROR: MBC not implemented", MessageBoxButtons.OK, MessageBoxIcon.Error); }
            else
            {
                clock = new Clock(new CPU(rom));
                clockTread = new Thread(clock.RunCPU);
                clockTread.Start();
            }

        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                LoadData(openFileDialog1.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutForm af = new AboutForm();
            af.Show();
        }

        private void cartrigeInfoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileOpened)
            {
                CartrigeInfoForm cartif = new CartrigeInfoForm();

                cartif.SetText(rom.ToString());
                cartif.Show();
            }
            else
            {
                MessageBox.Show("First open a valid cartrige file", "ERROR: No cartrige in memory", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dissassemblyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (fileOpened)
            {
                Disassembly ds = new Disassembly(rom);
                ds.Show();
            }
            else
            {
                MessageBox.Show("First open a valid cartrige file", "ERROR: No cartrige in memory", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_FormClosing(object sender, EventArgs e)
        {
            if (clockTread != null)
                clockTread.Abort();
        }


    }
}
